<?php
/**
 * jishupu-cms
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2021/11/11
 */

namespace App\Http\Controllers;

use App\Filters\LinksFilter;
use App\Http\Requests\LinksRequest;
use App\Http\Resources\LinksResource;
use App\Http\Resources\LinksCollection;
use App\Models\Links;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 友情链接模块
 * Class LinksController
 * @package App\Http\Controllers
 */
class LinksController extends Controller
{

    /**
     * 列表
     * @param LinksFilter $filter
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(LinksFilter $filter)
    {
        $paginate = Links::filter($filter)->paginate($this->pageSize());
        $result = LinksCollection::make($paginate)->toArray(null);
        return $this->success('', $result);
    }

    /**
     * 添加
     * @param LinksRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(LinksRequest $request)
    {
        $data = $request->validate('store');
        $links = new Links($data);
        if (!$links->save()) return $this->errorStored();
        return $this->successStored();
    }

    /**
     * 查看
     * @param Links $link
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(Links $link)
    {
        $link = LinksResource::make($link)->except(['created_at', 'updated_at'])->toArray(null);
        return $this->success('', $link);
    }


    /**
     * 修改
     * @param LinksRequest $request
     * @param Links $link
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function update(LinksRequest $request, Links $link)
    {
        $data = $request->validate('update');
        if (!$link->update($data)) return $this->errorUpdated();
        return $this->successUpdated();
    }

    /**
     * 删除
     * @param $id // 1或'1,2'
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id)
    {
        DB::beginTransaction();
        try {
            $between = explode(',', $id);
            Links::destroy($between);
            DB::commit();
            return $this->successDestoryed();
        } catch (\Exception $e) {
            DB::rollback();
            return $this->exception($e->getMessage())->errorDestoryed();
        }
    }

    /**
     * 更新状态
     * @param LinksRequest $request
     * @param Links $link
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function updateStatus(LinksRequest $request, Links $link)
    {
        $data = $request->validate('updateStatus');
        if (!$link->update($data)) return $this->errorUpdated();
        return $this->successUpdated();
    }

    /**
     * 排序
     * @param LinksRequest $request
     * @param Links $link
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function updateSort(LinksRequest $request, Links $link)
    {
        $data = $request->validate('updateSort');
        if (!$link->batchUpdate($data['sort'])) return $this->errorUpdated();
        return $this->successUpdated();
    }
}
